<?php
namespace app\manage\controller;
use app\common\logic\Send;
class User extends \think\Controller
{
    /**
     * 发送登陆短信
     * @param  string  $telephone  接受短信手机号
     * @return string  $token      加密后的验证码
     */
    public function send(){
        $telephone = input('param.telephone');
        $send = new Send();
        return $send -> code($telephone,mt_rand(100000,999999),'manage');
    }

    /**
     * 验证登陆短信
     * @param  string  $telephone  用户填写的手机号
     * @param  string  $code       用户填写的验证码
     * @return bool
     */
    private function checkSend($telephone='',$code =''){
        $cacheName = $telephone.'manage';
        if(checkTel($telephone) !== true)
            return checkTel($telephone);
        if (!$code)
            return [
                'message' => '验证码不能为空'
            ];
        if (!cache($cacheName))
            return [
                'message' => '验证码不正确'
            ];
        if (cache($cacheName)['num'] >=5)
            return [
                'message' => '验证次数过多，请一小时后再试'
            ];

        if ($code == cache($cacheName)['code'])
            return true;

        $data = cache($cacheName);
        $data['num'] +=1;
        cache($cacheName,$data,3600);
        return [
            'message' => '验证码不正确'
        ];
    }

    /**
     * 验证登陆
     * @param  string  $data['telephone']  用户填写的手机号
     * @param  string  $data['code']       用户填写的验证码
     * @return string  $token
     */
    public function login(){
        $data = input('param.');
        $check = $this -> checkSend($data['telephone'],$data['code']);
        if ($check !== true)
            return $check;
        //验证成功
        $info = \app\common\model\Manage::where([
            'telephone' => $data['telephone']
        ])->find();
        if (!$info)
            return ['message'=> '该账号不存在，登录失败'];
        $token = dataSign([
            'auth' => 'manage',
            'telephone' => $info['telephone'],
            'date' => time()
        ]);
        //存入缓存
        cache($token,[
            'auth' => 'manage',
            'telephone' => $info['telephone'],
            'time' => time()
        ],3600*24*7);
        cookie('manageAuth',$token,3600*24*7);
        cache($data['telephone'].'manage',null);
        return [
            'code' => '1',
            'token' => $token
        ];
    }

    //登陆页
    public function index(){
        if ($this -> isLogin())
            $this -> redirect('./manage/index');
        return view();
    }

    //判断是否登陆
    protected function isLogin(){
        $info = cache(cookie('manageAuth'));
        $check = \app\common\model\Manage::where('telephone',$info['telephone'])->find();
        if ($info['auth'] == 'manage' && $check)
            return true;
    }

    public function logout(){
        cache(cookie('manageAuth'),null);
        cookie('manageAuth',null);
        $this -> redirect('./manage/user/index');
    }

    public function ceshi(){
//        dump(cache('18191027139manage'/));
//        dump(cache('18829343042manage'));
    }
    public function _empty()
    {
        //把所有城市的操作解析到city方法
        $this -> redirect('user/index');
    }


}